package com.ax.business.hemp.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ax.business.common.util.MapToBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.business.common.util.PageUtils;
import com.ax.business.common.util.Query;
import com.ax.business.hemp.dao.HempRegisteMastMapper;
import com.ax.business.hemp.service.HempRegisteService;
import com.ax.business.hemp.dao.HempRegisteDetailMapper;
import com.ax.business.hemp.domain.HempRegisteDetail;
import com.ax.business.hemp.domain.HempRegisteMast;
import org.springframework.transaction.annotation.Transactional;

@Service("HempRegisteService")
public class HempRegisteServiceImpl implements HempRegisteService {

    @Autowired  
    HempRegisteMastMapper HempRegisteMastMapper;

    @Autowired
    HempRegisteDetailMapper HempRegisteDetailMapper;
    
    /**
	 * 含麻登记(模块:含麻)
	 * 
	 * @param HempRegisteMast hempRegisteMast
	 *           
	 * @return SUCC
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertHempSer(Map<String,Object> map,String hempRegisteSeq){
		List<String> parameter = new ArrayList<>();
		if(map.get("parameter[]").toString() != null && map.get("parameter[]").toString().contains(",")){
			parameter = Arrays.asList(map.get("parameter[]").toString().split(","));
		}
		parameter.add(map.get("parameter[]").toString());
		HempRegisteMast hempRegisteMast = MapToBean.toBean(HempRegisteMast.class,map);
		HempRegisteMastMapper.insertSelective(hempRegisteMast);
        //0.药品ID,1.药品名称,2.规格,3.价格,4.数量,5.金额,6.备注	
		HempRegisteDetail hempRegisteDetail = new HempRegisteDetail();
		hempRegisteDetail.setHempRegisteId(hempRegisteMast.getHempRegisteId());
		hempRegisteDetail.setHempRegisteSeq(hempRegisteSeq);
		List<HempRegisteDetail> hempRegisteDetails = new ArrayList<>();
		for(String pt : parameter){
			String[] hempDetails = pt.split("///");
			hempRegisteDetail.setDrugId(Long.parseLong(hempDetails[0]));
			hempRegisteDetail.setDrugName(hempDetails[1]);
			hempRegisteDetail.setSpec(hempDetails[2]);
			hempRegisteDetail.setNumber(Float.parseFloat(hempDetails[3]));
			hempRegisteDetail.setFlag(1);
			hempRegisteDetails.add(hempRegisteDetail);
		}
		System.out.println(hempRegisteDetails);
		HempRegisteDetailMapper.insertHempRegisteDetail(hempRegisteDetails);
	}

	/**
	 * 含麻查询(模块:含麻)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return PageUtils
	 */
	@Override
	public PageUtils queryHempSer(Map<String,Object> map){
		Query query = new Query(map);
		List<HempRegisteMast> HempRegisteMasts = HempRegisteMastMapper.queryHempMapper(query);
		Integer count = HempRegisteMastMapper.count(query);
		PageUtils pageUtil = new PageUtils(HempRegisteMasts,count);
		return pageUtil;
	}

	/**
	 * 禁用启用含麻详细
	 * @param flag
	 * @param hempRegisteDetailId
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer updateFlag(Integer flag, String[] hempRegisteDetailId) {
		return HempRegisteDetailMapper.updateFlag(flag,hempRegisteDetailId);
	}

	/**
	 * 修改含麻详细信息
	 * @param hempRegisteDetail
	 * @return
	 */
	@Override
	public Integer updateHempRegisteDetail(HempRegisteDetail hempRegisteDetail) {
		return HempRegisteDetailMapper.updateByPrimaryKeySelective(hempRegisteDetail);
	}

	/**
	 * 删除含麻详细信息
	 * @param hempRegisteDetailId
	 * @return
	 */
	@Override
	public Integer deleteHempRegisteDetail(String[] hempRegisteDetailId) {
		return HempRegisteDetailMapper.deleteByPrimaryKey(hempRegisteDetailId);
	}

}
